package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Tests the sanity report feature.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author JianFei Liao
 * @author Jared Sunouchi
 */
public class TestSanityReport {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string representing the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 2.0 - Home";
  /** A string representing the login page title */
  private static final String LOGIN_TITLE = "MyISERN 2.0 - Login";
  /** A string represent the username attribute */
  private static final String USERNAME_FIELD = "username";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  /** A string representing the user name */
  private static final String USERNAME = "username";
  /** A string representing the password */
  private static final String PASSWORD = "password";
  
  
  /**
   * Tests the sanity report feature.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testSanityReport() throws Exception {    
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Clicking link without login", LOGIN_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(USERNAME_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Logged in", HOMEPAGE_TITLE, response.getTitle());
    
    WebLink link = response.getLinkWithID("sanityReport");
    response = link.click();
    assertEquals("Checking report page", "MyISERN 2.0 - Sanity Report", response.getTitle());
    
    WebLink[] allLinks = response.getLinks();
    for (WebLink webLink : allLinks) {
      webLink.click();
    }
    
    link = response.getLinkWithID("logout");
    link.click();
    response = conversation.getCurrentPage();
    assertEquals("Logged out", LOGIN_TITLE, response.getTitle());
  }    
}
